<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="店铺名称" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="店铺名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="stateApply">
        <el-select v-model="queryParams.stateApply" placeholder="审核状态" clearable >
          <el-option v-for="dict in storeStateList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="营业状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="营业状态" clearable>
          <el-option v-for="dict in stateList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" >搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="storeList">
      <el-table-column label="店铺ID" align="center" prop="id" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="店铺LOGO" width="120" align="center" prop="logo">
        <template slot-scope="scope">
          <el-image v-if="scope.row.logo" :src="scope.row.logo" :preview-src-list="[scope.row.logo]" style="width: 50px;height: 50px;object-fit: cover" />
          <div style="height: 57px; line-height: 57px" v-else>-</div>
        </template>
      </el-table-column>
      <el-table-column label="店铺封面图" width="120" align="center" prop="mainImgUrl">
        <template slot-scope="scope">
          <el-image v-if="scope.row.mainImgUrl" :src="scope.row.mainImgUrl" :preview-src-list="[scope.row.mainImgUrl]" style="width: 50px;height: 50px;object-fit: cover" />
          <div style="height: 57px; line-height: 57px" v-else>-</div>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="审核状态" align="center" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <div>{{ scope.row.stateApply | getLabel(storeStateList) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="applyRemark" />
      <el-table-column label="审核时间" align="center" prop="applyTime" width="100" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="营业状态" align="center" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <div>{{ scope.row.state | getLabel(stateList) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="服务费" align="center" prop="serviceCharge">
        <template slot-scope="scope">
          <div>{{ Number(scope.row.serviceCharge)*100 }}%</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCheck(scope.row)" v-if="scope.row.stateApply == 1">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
    <el-dialog title="审核店铺" :visible.sync="checkOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="店铺名称" prop="storeName">
          <el-input :value="form.storeName" readonly />
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-radio-group v-model="form.auditStatus">
            <el-radio v-for="dict in storeStateApply" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemarks" v-if="form.auditStatus === 2" >
          <el-input v-model="form.auditRemarks" type="textarea" :maxlength="50" show-word-limit rows="4" placeholder="请输入审核备注"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listStore, delStore, checkStoreRefuse, checkStorePass } from "@/api/store/store";
export default {
  name: "Store",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺表格数据
      storeList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        storeName: null,
        stateApply: null,
        state: null,
        mobile: "",
      },
      storeStateList:[
        {value: 1, label: '审核中'},
        {value: 2, label: '审核通过'},
        {value: 3, label: '审核驳回'}
      ],
      stateList:[
        {value: 1, label: '正常营业'},
        {value: 0, label: '停止营业'}
      ],
      rules:{
        auditRemarks: [
          { required: true, message: "请输入审核拒绝原因", trigger: "blur" },
        ],
        auditStatus: [
          { required: true, message: "请选择审核状态", trigger: "change" },
        ],
      },
      // 店铺审核
      storeStateApply: [
        { value: 1, label: "通过" },
        { value: 2, label: "驳回" },
      ],
      form: {
        id: "", //审核id
        auditStatus: "",
        auditRemarks: "",//审核备注
      },
      checkOpen: false,
    };
  },
  filters:{
    getLabel(val,statusList){
      let label;
      let arr = statusList.filter(item => item.value == val)
      if(arr.length){
        label = arr[0].label
      }
      return label;
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询店铺列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then((res) => {
        if(res.code === 200){
          this.storeList = res.data;
          this.total = res.extend.total;
          this.loading = false;
        }
      });
    },
    //店铺审核
    handleCheck(data){
      this.form = JSON.parse(JSON.stringify(data))
      this.checkOpen = true
      this.$nextTick(()=>{
        this.$refs['form'].clearValidate();
      })
    },
    // 取消按钮
    cancel() {
      this.checkOpen = false;
      this.form = {
        id: "", //审核id
        auditStatus: "",
        auditRemarks: "",
      }
    },
    // 审核
    submitForm(){
      let that = this
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let txt = that.form.auditStatus == 1 ?'通过': '拒绝';
          that.$modal.confirm('是否确认'+txt+'该申请？').then(function() {
            if(that.form.auditStatus==1){
              let parmas = {
                id: that.form.id
              }
              checkStorePass(parmas).then(res => {
                if(res.code === 200){
                  that.$modal.msgSuccess("操作成功");
                  that.checkOpen = false
                  that.form = {}
                  that.getList();
                }
              })
            }else{
              let parmas = {
                id: that.form.id,
                applyRemark: that.form.auditRemarks,
              }
              checkStoreRefuse(parmas).then(res => {
                if(res.code === 200){
                  that.$modal.msgSuccess("操作成功");
                  that.checkOpen = false
                  that.auditRemarks = null;
                  that.form = {}
                  that.getList();
                }
              })
            }
          })
        }
      })
    },
    //新增修改成功
    success() {
      this.open = false;
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        pageNo: 1,
        pageSize: 10,
        storeName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除店铺名称为"' + row.storeName + '"的商家？').then(function () {
          return delStore(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.content {
  white-space: pre-wrap;
  max-width: 200px;
  line-height: 20px;
}
</style>
